//
//  ZhuceViewController.m
//  维度科技
//
//  Created by 谷银行 on 2019/1/25.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "ZhuceViewController.h"
#import "RSAEncryptor.h"

@interface ZhuceViewController ()<UITextFieldDelegate>{
    // 背景图
    UIImageView *img;
    // 昵称
    UIView *vie;
    UITextField *textField;
    //手机号
    UIView *Sjh;
    UITextField *SjtextField;
    //密码
    UIView *Mima;
    UITextField *MitextField;

    
}
// Logo
@property(nonatomic,strong)UIImageView *headimg;
@end

@implementation ZhuceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 背景图
    img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, HEIGHT)];
    img.image=[UIImage imageNamed:@"Bitmap"];
    [self.view addSubview:img];
    
    // Logo
    [self Logo];
    // 昵称
    [self NiCheng];
    // 手机号
    [self ShouJi];
    // 密码
    [self DLMima];
    // 短信注册
    [self SMSSZhuce];
    // 注册
    [self Zhuce];
    
}
// Logo
-(void)Logo{
    _headimg = [[UIImageView alloc]init];
    [self.view addSubview:self.headimg];
    _headimg.sd_layout
    .topSpaceToView(self.view, 120)
    .leftSpaceToView(self.view, (Width - 80)/2)
    .heightIs(80)
    .widthIs(80);
    _headimg.layer.masksToBounds=YES;
    _headimg.layer.cornerRadius=40;
    _headimg.image=[UIImage imageNamed:@"icon_320"];
}
// 昵称
- (void)NiCheng{
    vie=[[UIView alloc] init];
    [self.view addSubview:vie];
    vie.sd_layout
    .topSpaceToView(self.headimg, 40)
    .leftSpaceToView(self.view, (Width - 280)/2)
    .heightIs(40)
    .widthIs(280);
    vie.layer.borderWidth = 1;
    vie.layer.borderColor = [UIColor whiteColor].CGColor;
    
    
    UIImageView *ren=[[UIImageView alloc] init];
    [vie addSubview:ren];
    ren.sd_layout
    .topSpaceToView(vie, 10)
    .leftSpaceToView(vie, 10)
    .heightIs(20)
    .widthIs(20);
    ren.image=[UIImage imageNamed:@"register_icon_name_n_hdpi"];
    
    
    textField = [[UITextField alloc]init];
    [vie addSubview:textField];
    textField.delegate = self;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.sd_layout
    .topSpaceToView(vie, 0)
    .leftSpaceToView(ren, 10)
    .heightIs(40)
    .widthIs(220);
    textField.textColor = [UIColor whiteColor];
    textField.placeholder = @"昵称";
}
// 手机号
- (void)ShouJi{
    Sjh=[[UIView alloc] init];
    [self.view addSubview:Sjh];
    Sjh.sd_layout
    .topSpaceToView(vie, 40)
    .leftSpaceToView(self.view, (Width - 280)/2)
    .heightIs(40)
    .widthIs(280);
    Sjh.layer.borderWidth = 1;
    Sjh.layer.borderColor = [UIColor whiteColor].CGColor;
    
    UIImageView *Sj=[[UIImageView alloc] init];
    [Sjh addSubview:Sj];
    Sj.sd_layout
    .topSpaceToView(Sjh, 10)
    .leftSpaceToView(Sjh, 10)
    .heightIs(20)
    .widthIs(20);
    Sj.image=[UIImage imageNamed:@"register_icon_name_n_hdpi"];
    
    
    SjtextField = [[UITextField alloc]init];
    [Sjh addSubview:SjtextField];
    SjtextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    SjtextField.delegate = self;
    SjtextField.sd_layout
    .topSpaceToView(Sjh, 0)
    .leftSpaceToView(Sj, 10)
    .heightIs(40)
    .widthIs(220);
    SjtextField.textColor = [UIColor whiteColor];
    SjtextField.placeholder = @"手机号";
    
}
// 密码
- (void)DLMima{
    Mima=[[UIView alloc] init];
    [self.view addSubview:Mima];
    Mima.sd_layout
    .topSpaceToView(Sjh, 40)
    .leftSpaceToView(self.view, (Width - 280)/2)
    .heightIs(40)
    .widthIs(280);
    Mima.layer.borderWidth = 1;
    Mima.layer.borderColor = [UIColor whiteColor].CGColor;
    
    UIImageView *Mi=[[UIImageView alloc] init];
    [Mima addSubview:Mi];
    Mi.sd_layout
    .topSpaceToView(Mima, 10)
    .leftSpaceToView(Mima, 10)
    .heightIs(20)
    .widthIs(20);
    Mi.image=[UIImage imageNamed:@"register_icon_name_n_hdpi"];
    
    MitextField = [[UITextField alloc]init];
    [Mima addSubview:MitextField];
    MitextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    MitextField.delegate = self;
    MitextField.sd_layout
    .topSpaceToView(Mima, 0)
    .leftSpaceToView(Mi, 10)
    .heightIs(40)
    .widthIs(220);
    MitextField.textColor = [UIColor whiteColor];
    MitextField.secureTextEntry = YES;
    MitextField.placeholder = @"登录密码";
}

- (void)SMSSZhuce{
    UIButton *SMSSBtn = [[UIButton alloc]init];
    [self.view addSubview:SMSSBtn];
    SMSSBtn.sd_layout
    .topSpaceToView(Mima, 10)
    .rightSpaceToView(self.view, 40)
    .heightIs(30)
    .widthIs(150);
    [SMSSBtn setTitle:@"短信验证登录" forState:UIControlStateNormal];
    SMSSBtn.font = [UIFont systemFontOfSize:14 weight:1];
}
- (void)Zhuce{
    UIButton *Btn = [[UIButton alloc]init];
    [self.view addSubview:Btn];
    Btn.sd_layout
    .bottomSpaceToView(self.view, 96)
    .rightSpaceToView(self.view, ((Width - 280)/2))
    .heightIs(40)
    .widthIs(280);
    [Btn setTitle:@"注册" forState:UIControlStateNormal];
    [Btn addTarget:self action:@selector(ZhuceBtn) forControlEvents:UIControlEventTouchUpInside];
    Btn.font = [UIFont systemFontOfSize:18 weight:1];
    Btn.backgroundColor = [UIColor purpleColor];
    Btn.layer.masksToBounds=YES;
    Btn.layer.cornerRadius=8;
}
- (void)ZhuceBtn{
    if ([SjtextField.text isEqualToString:@""] && [MitextField.text isEqualToString:@""]) {
        NSLog(@"没有值");
    }else{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
        securityPolicy.validatesDomainName = NO;
        securityPolicy.allowInvalidCertificates = YES;
        manager.securityPolicy = securityPolicy;
        NSString *SJstr =SjtextField.text;
        NSString *TextStr =textField.text;
        // 进行加密
        NSString *originalString = MitextField.text;
    
//            使用字符串格式的公钥私钥加密解密
        NSString *encryptStr = [RSAEncryptor encryptString:originalString publicKey:@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuQd3gESx7VdIyRYUWjmjg61VIgUK6F45hClmqUMZ7xNiT5rlLM6e78osMvBF/yP7cVm7pK+NMCDWoVS1/AETpxJYqUlIC77ZAU8/MnP96IupnJL87vqhPcpdv7+VqLM38ls++yuD/F/HSoOQTv/leJh+dgE/4EYAJjOWFAbYfXwIDAQAB"];

            NSLog(@"加密前:%@", originalString);
            NSLog(@"加密后:%@", encryptStr);
        //参数 电影
        NSDictionary *dic = @{
                              @"phone":SJstr,
                              @"nickName":TextStr,
                              @"pwd":encryptStr,
                              };
        [manager POST:@"https://172.17.8.100/techApi/user/v1/register" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"数据有,%@",responseObject);
            NSLog(@"-----%@",responseObject[@"message"]);
            if ([responseObject[@"message"] isEqualToString:@"注册成功"]) {
                UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:responseObject[@"message"] preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *action  = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    DengLuViewController *dl = [DengLuViewController new];
                    [self presentViewController:dl animated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"失败" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *action  = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    
    }

}


@end
